<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="userDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="用户名称">
							<el-input v-model="state.ruleForm.userName" placeholder="请输入账户名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="登录账户">
							<el-input v-model="state.ruleForm.userAccount" placeholder="请输入用户昵称" clearable></el-input>
						</el-form-item>
					</el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="登录密码">
              <el-input v-model="state.ruleForm.userPassword"  type="password"  placeholder="请输入用户密码" clearable></el-input>
            </el-form-item>
          </el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="所属单位">
              <el-select v-model="state.ruleForm.belongUnit" placeholder="请选择" clearable class="w100"
               @focus ="onFetchUserTypes"
              >
                <el-option
                    v-for="option in state.userTypeOptions"
                    :key="option.id"
                    :label="option.name"
                    :value="option.id"
                ></el-option>
              </el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注">
							<el-input v-model="state.ruleForm.contextNotes" type="textarea" placeholder="请输入备注" maxlength="150"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemUserDialog">
import { reactive, onMounted,ref} from 'vue';
import {useBussApi} from "/@/api/buss/buss";

const bussApi = useBussApi();

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const userDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		userName: '', // 账户名称
    userAccount: '', // 用户昵称
    userPassword: '', // 账户密码、
    belongUnit: '',
    contextNotes: '',
    id: null,
	},
  userTypeOptions: [],
	deptData: [] as DeptTreeType[], // 部门数据
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, row: RowUserType) => {
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改用户';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增用户';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		// nextTick(() => {
		// 	userDialogFormRef.value.resetFields();
		// });
	}
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
  state.ruleForm = {
    userName: '', // 账户名称
    userAccount: '', // 用户昵称
    userPassword: '', // 账户密码、
    belongUnit: '',
    contextNotes: '',
    id: null,
  }
	closeDialog();
};

const fetchUserTypeOptions = async () => {
  try {
    const res = await bussApi.queryUserTypeList('');
    state.userTypeOptions = res.data; // 假设后端返回的数据格式为 [{ label: '角色1', value: 'role1' }, ...]
    console.log(state.userTypeOptions)
  } catch (error) {
    console.error('获取角色数据失败', error);
  }
};
// 提交
const onSubmit = async () => {
  const res = await bussApi.editUser(state.ruleForm);
	closeDialog();
  state.ruleForm = {
    userName: '', // 账户名称
    userAccount: '', // 用户昵称
    userPassword: '', // 账户密码、
    belongUnit: '',
    contextNotes: '',
    id: null,
  }
	emit('refresh');
	// if (state.dialog.type === 'add') { }
};


// 暴露变量
defineExpose({
	openDialog,
});
onMounted(() => {
  // 在这里执行异步操作
  fetchUserTypeOptions();
});

const  onFetchUserTypes = async () => {
  fetchUserTypeOptions();
}
</script>
